<template lang="pug">
  div
    app-url
    taobao-pswd(ref="taobaoPswd", v-if="isWeiXin", :good="good")
    loading(v-if="loading")
    good-card(:good="good")
    .gray-line(v-if="good.title || loading")
    error-card(:error="error",v-if="error")
    coupons-list(:coupons="coupons")
    divider 猜你喜欢
    goods-list(:goods="recommend")
    packet-handler
</template>

<script>
import GoodCard from './components/GoodCard'
import CouponsList from './components/CouponsList'
import AppUrl from './components/AppUrl'
import Loading from './components/Loading'
import ErrorCard from './components/ErrorCard'
import TaobaoPswd from './components/TaobaoPswd'
import Window from './helpers/window'
import WxHelper from './helpers/wx_helper'
import GoodsList from './components/GoodsList'
import Divider from './components/Divider'
import PacketHandler from './components/PacketHandler'
import SiteInfo from './helpers/site_info'

export default {
  name: 'GoodDetail',
  components: {
    GoodCard,
    CouponsList,
    Loading,
    ErrorCard,
    TaobaoPswd,
    GoodsList,
    Divider,
    PacketHandler,
    AppUrl
  },
  data () {
    return {
      good: {},
      recommend: [],
      coupons: [],
      error: null,
      loading: true
    }
  },
  watch: {
    '$route': function (to, from) {
      this.loadGood()
    }
  },
  computed: {
    isWeiXin () {
      return Window.isWechat()
    },
    site_info () {
      return SiteInfo.get()
    }
  },
  methods: {
    loadGood () {
      if (this.$route.params.id) {
        this.getGood()
      } else if (this.$route.query.url) {
        this.searchGood()
      } else {
        this.error = '抱歉,没有该商品优惠券!'
      }
    },
    searchGood () {
      this.$http.get('/proxy/api/coupons/search', {params: {url: this.$route.query.url}}).then(this.goodResponse, this.goodError)
    },
    getGood () {
      this.$http.get(`/proxy/api/dataoke_goods/${this.$route.params.id}`).then(this.goodResponse, this.goodError)
    },
    goodResponse (res) {
      this.loading = false
      if (res.body && res.body && res.body.error) {
        this.error = '抱歉,没有该商品优惠券!'
        return false
      }

      this.good = res.data
      this.coupons = res.data.coupons
      this.recommend = res.data.recommend
      if (this.coupons.length === 0) {
        this.error = '你来晚了,优惠券抢完了'
        this.signWechat(this.site_info.title)
      } else {
        this.error = null
        this.signWechat(`现价:￥${this.good.zkPrice} 领券后:￥ ${this.good.discount_price}`)
      }
    },
    goodError () {
      this.loading = false
      this.error = '你来晚了,优惠券抢完了'
    },
    signWechat (desc) {
      WxHelper.share(this.$wechat, this.good.appid, this.good.jsapi_ticket, window.location.href, {
        title: this.good.title,
        desc: desc,
        link: window.location.url,
        imgUrl: this.good.pictUrl
      })
    }
  },
  mounted () {
    this.loadGood()
  }
}
</script>

<style lang="sass">
@import './styles/color.sass';
.gray-line
  background: $gray-bg
  height: 15px
.no-data-base
  padding-top: 150px
  text-align: center
  padding-bottom: 100px
  color: $gray
  font-size: 1em
</style>
